﻿using System;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Interactivity.InteractionRequest;
using Mitosoft.Definitions.ViewModels;
using TextFileReaderModule.Notifications;

namespace TextFileReaderModule.ViewModels
{
  /// <summary>
  /// View model used to reflect the dialog asking the user the line number to go.
  /// </summary>
  public class GotoLineNumberDialogViewModel : BaseViewModel, IInteractionRequestAware
  {
    #region fields

    /// <summary>
    /// Represents <see cref="GotoLineNumberDialogNotification"/>
    /// </summary>
    protected GotoLineNumberDialogNotification _gotoLineNumberDialogNotification;

    /// <summary>
    /// Represents <see cref="LineNumber"/>
    /// </summary>
    protected uint _lineNumber;

    #endregion

    #region constructor(s)

    /// <summary>
    /// Creates a new <see cref="GotoLineNumberDialogViewModel"/> instance.
    /// </summary>
    public GotoLineNumberDialogViewModel()
    {
      CancelDialogCommand = new DelegateCommand(CancelDialogCommandInteraction);
      AcceptDialogCommand = new DelegateCommand(AcceptDialogCommandInteraction);
    }

    #endregion

    #region properties

    /// <summary>
    /// Command used when the dialog was canceled.
    /// </summary>
    public ICommand CancelDialogCommand { get; private set; }

    /// <summary>
    /// Command used when the dialog was accepted.
    /// </summary>
    public ICommand AcceptDialogCommand { get; private set; }

    /// <summary>
    /// Gets or sets the line number.
    /// </summary>
    public uint LineNumber
    {
      get { return _lineNumber; }
      set
      {
        _lineNumber = value;

        HandlePropertyChanged();
      }
    }

    #endregion

    #region methods

    /// <summary>
    /// <see cref="CancelDialogCommand"/> was executed.
    /// </summary>
    protected void CancelDialogCommandInteraction()
    {
      if (Notification != null)
      {
        _gotoLineNumberDialogNotification.GotoLineNumber = null;
        _gotoLineNumberDialogNotification.Confirmed = false;
      }

      FinishInteraction();
    }

    /// <summary>
    /// <see cref="AcceptDialogCommand"/> was executed.
    /// </summary>
    protected void AcceptDialogCommandInteraction()
    {
      if (Notification != null)
      {
        _gotoLineNumberDialogNotification.GotoLineNumber = LineNumber;
        _gotoLineNumberDialogNotification.Confirmed = true;
      }

      FinishInteraction();
    }

    #endregion

    #region IInteractionRequestAware

    /// <summary>
    /// Gets or sets the Finish - action.
    /// </summary>
    public Action FinishInteraction
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets the Notification - action.
    /// </summary>
    public INotification Notification
    {
      get { return _gotoLineNumberDialogNotification; }
      set
      {
        if (!(value is GotoLineNumberDialogNotification)) return;
        _gotoLineNumberDialogNotification = value as GotoLineNumberDialogNotification;
        HandlePropertyChanged();
      }
    }

    #endregion
  }
}
